import 'package:flutter/material.dart';
import 'home_category_item.dart';
import 'package:flutter_cate/core/services/json_parse.dart';
import 'package:flutter_cate/core/model/category_model.dart';
import 'package:flutter_cate/core/extension/int_extension.dart';

class AMHomeContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {

    return FutureBuilder<List<AMCategoryModel>>(
        future: AMJsonParse.getCategodyData(),
        builder: (ctx, snapshot) {
          if (!snapshot.hasData) return Center(child: CircularProgressIndicator());
          if (snapshot.error != null) return Center(child: Text("网络请求失败"));

          final categories = snapshot.data;
          return GridView.builder(
              padding: EdgeInsets.all(20.px),
              itemCount: categories.length,
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 2,
                crossAxisSpacing: 20.px,
                mainAxisSpacing: 20.px,
                childAspectRatio: 1.5
              ),
              itemBuilder: (ctx ,index) {
                return AMHomeCategoryItem(categories[index]);
              }
          );
        }
    );
  }
}
